
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>WebSocket · 记录技术文档内容</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="zhengyuliang">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-sectionx/sectionx.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="npm常用命令大全.html" />
    
    
    <link rel="prev" href="../message/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">CSS</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../CSS/18种CSS命名和书写规范.html">
            
                <a href="../CSS/18种CSS命名和书写规范.html">
            
                    
                    18种CSS命名和书写规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../CSS/灵活运用CSS开发技巧.html">
            
                <a href="../CSS/灵活运用CSS开发技巧.html">
            
                    
                    灵活运用CSS开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../CSS/自适应 flex grid.html">
            
                <a href="../CSS/自适应 flex grid.html">
            
                    
                    自适应 Flex Grid
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">HTML</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../HTML/浏览器里的本地数据库：IndexedDB.html">
            
                <a href="../HTML/浏览器里的本地数据库：IndexedDB.html">
            
                    
                    浏览器里的本地数据库：IndexedDB
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Vue</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../Vue/7个有用的Vue开发技巧.html">
            
                <a href="../Vue/7个有用的Vue开发技巧.html">
            
                    
                    7个有用的Vue开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../Vue/Vue this.$router.push、replace、go的区别.html">
            
                <a href="../Vue/Vue this.$router.push、replace、go的区别.html">
            
                    
                    Vue This.$Router.Push、Replace、Go的区别
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../Vue/Vue 的父组件和子组件生命周期钩子函数执行顺序？.html">
            
                <a href="../Vue/Vue 的父组件和子组件生命周期钩子函数执行顺序？.html">
            
                    
                    Vue 的父组件和子组件生命周期钩子函数执行顺序？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../Vue/Vue.set和this.$set应用的场景.html">
            
                <a href="../Vue/Vue.set和this.$set应用的场景.html">
            
                    
                    Vue.Set和This.$Set应用的场景
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../Vue/vue高级技巧.html">
            
                <a href="../Vue/vue高级技巧.html">
            
                    
                    Vue高级技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../Vue/从 0 开始手把手带你搭建一套规范的 Vue3.x 项目工程环境.html">
            
                <a href="../Vue/从 0 开始手把手带你搭建一套规范的 Vue3.x 项目工程环境.html">
            
                    
                    从 0 开始手把手带你搭建一套规范的 Vue3.X 项目工程环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../Vue/浅析 vue-router 源码和动态路由权限分配.html">
            
                <a href="../Vue/浅析 vue-router 源码和动态路由权限分配.html">
            
                    
                    浅析 Vue Router 源码和动态路由权限分配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../Vue/父组件监听到子组件的生命周期.html">
            
                <a href="../Vue/父组件监听到子组件的生命周期.html">
            
                    
                    父组件监听到子组件的生命周期
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="../Vue/理解Vue.mixin，利用Vue.mixin正确的偷懒.html">
            
                <a href="../Vue/理解Vue.mixin，利用Vue.mixin正确的偷懒.html">
            
                    
                    理解Vue.Mixin，利用Vue.Mixin正确的偷懒
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">JavaScript</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../javaScript/5个技巧让你更好的编写 JavaScript(ES6">
            
                <span>
            
                    
                    5个技巧让你更好的编写 JavaScript(ES6) 中条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../javaScript/5种常见函数的写法和调用方式.html">
            
                <a href="../javaScript/5种常见函数的写法和调用方式.html">
            
                    
                    5种常见函数的写法和调用方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../javaScript/7道javascript题，让你理解基本的javascript.html">
            
                <a href="../javaScript/7道javascript题，让你理解基本的javascript.html">
            
                    
                    7道Javascript题，让你理解基本的Javascript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../javaScript/Array.from(">
            
                <span>
            
                    
                    Array.From() 五个超好用的用途
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../javaScript/ES6汇总资料.html">
            
                <a href="../javaScript/ES6汇总资料.html">
            
                    
                    ES6汇总资料
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="../javaScript/JS开发必须知道的41个技巧.html">
            
                <a href="../javaScript/JS开发必须知道的41个技巧.html">
            
                    
                    JS开发必须知道的41个技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="../javaScript/JavaScript之防抖Debounce和节流Throttle.html">
            
                <a href="../javaScript/JavaScript之防抖Debounce和节流Throttle.html">
            
                    
                    JavaScript之防抖Debounce和节流Throttle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="../javaScript/JavaScript数组求并集，交集和差集.html">
            
                <a href="../javaScript/JavaScript数组求并集，交集和差集.html">
            
                    
                    JavaScript数组求并集，交集和差集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="../javaScript/JavaScript的优雅写法.html">
            
                <a href="../javaScript/JavaScript的优雅写法.html">
            
                    
                    JavaScript的优雅写法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="../javaScript/axios知识点.html">
            
                <a href="../javaScript/axios知识点.html">
            
                    
                    Axios知识点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.11" data-path="../javaScript/javascript核心知识.html">
            
                <a href="../javaScript/javascript核心知识.html">
            
                    
                    Javascript核心知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.12" data-path="../javaScript/this、apply、call、bind.html">
            
                <a href="../javaScript/this、apply、call、bind.html">
            
                    
                    This、Apply、Call、Bind
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.13" data-path="../javaScript/前端js常用的60余种工具方法.html">
            
                <a href="../javaScript/前端js常用的60余种工具方法.html">
            
                    
                    前端Js常用的60余种工具方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.14" data-path="../javaScript/前端异常的捕获与处理.html">
            
                <a href="../javaScript/前端异常的捕获与处理.html">
            
                    
                    前端异常的捕获与处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.15" data-path="../javaScript/在 JavaScript 中交换值的 10 种方法.html">
            
                <a href="../javaScript/在 JavaScript 中交换值的 10 种方法.html">
            
                    
                    在 JavaScript 中交换值的 10 种方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.16" data-path="../javaScript/常用的前端JavaScript方法封装.html">
            
                <a href="../javaScript/常用的前端JavaScript方法封装.html">
            
                    
                    常用的前端JavaScript方法封装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.17" data-path="../javaScript/异步神器Async-await介绍与填坑.html">
            
                <a href="../javaScript/异步神器Async-await介绍与填坑.html">
            
                    
                    异步神器Async Await介绍与填坑
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.18" data-path="../javaScript/灵活运用JS开发技巧.html">
            
                <a href="../javaScript/灵活运用JS开发技巧.html">
            
                    
                    灵活运用JS开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.19" data-path="../javaScript/编写高质量可维护的代码：一目了然的注释.html">
            
                <a href="../javaScript/编写高质量可维护的代码：一目了然的注释.html">
            
                    
                    编写高质量可维护的代码：一目了然的注释
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.20" data-path="../javaScript/编写高质量可维护的代码：优化逻辑判断.html">
            
                <a href="../javaScript/编写高质量可维护的代码：优化逻辑判断.html">
            
                    
                    编写高质量可维护的代码：优化逻辑判断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.21" data-path="../javaScript/编写高质量可维护的代码：优雅命名.html">
            
                <a href="../javaScript/编写高质量可维护的代码：优雅命名.html">
            
                    
                    编写高质量可维护的代码：优雅命名
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.22" data-path="../javaScript/编写高质量可维护的代码：数据建模.html">
            
                <a href="../javaScript/编写高质量可维护的代码：数据建模.html">
            
                    
                    编写高质量可维护的代码：数据建模
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Life Exp</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../life_exp/告别平庸的15个小方法.html">
            
                <a href="../life_exp/告别平庸的15个小方法.html">
            
                    
                    告别平庸的15个小方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../life_exp/日志记录.html">
            
                <a href="../life_exp/日志记录.html">
            
                    
                    日志记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../life_exp/程序员的发展方向.html">
            
                <a href="../life_exp/程序员的发展方向.html">
            
                    
                    程序员的发展方向
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../message/">
            
                <a href="../message/">
            
                    
                    Message
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Technology</li>
        
        
    
        <li class="chapter active" data-level="6.1" data-path="WebSocket.html">
            
                <a href="WebSocket.html">
            
                    
                    WebSocket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="npm常用命令大全.html">
            
                <a href="npm常用命令大全.html">
            
                    
                    Npm常用命令大全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="智能照明项目说明.html">
            
                <a href="智能照明项目说明.html">
            
                    
                    智能照明项目说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="规范化git的提交信息.html">
            
                <a href="规范化git的提交信息.html">
            
                    
                    规范化Git的提交信息
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Tutorial</li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../tutorial/Esmap地图详解.html">
            
                <a href="../tutorial/Esmap地图详解.html">
            
                    
                    Esmap地图详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="../tutorial/npm镜像切换.html">
            
                <a href="../tutorial/npm镜像切换.html">
            
                    
                    Npm镜像切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="../tutorial/发布npm的教程.html">
            
                <a href="../tutorial/发布npm的教程.html">
            
                    
                    发布Npm的教程
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >WebSocket</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <html><head></head><body><div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#websocket"><b>1. </b>WebSocket</a></li><ul><ul><li><span class="title-icon "></span><a href="#&#x4E00;&#x3001;&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;-websocket&#xFF1F;"><b>1.1.1. </b>&#x4E00;&#x3001;&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981; WebSocket&#xFF1F;</a></li><li><span class="title-icon "></span><a href="#&#x4E8C;&#x3001;&#x7B80;&#x4ECB;"><b>1.1.2. </b>&#x4E8C;&#x3001;&#x7B80;&#x4ECB;</a></li><li><span class="title-icon "></span><a href="#&#x4E09;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x7B80;&#x5355;&#x793A;&#x4F8B;"><b>1.1.3. </b>&#x4E09;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x7B80;&#x5355;&#x793A;&#x4F8B;</a></li><li><span class="title-icon "></span><a href="#&#x56DB;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684;-api"><b>1.1.4. </b>&#x56DB;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684; API</a></li></ul></ul></ul></div><a href="#websocket" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="websocket"><a name="websocket" class="anchor-navigation-ex-anchor" href="#websocket"><i class="fa fa-link" aria-hidden="true"></i></a>1. WebSocket</h1>
<blockquote>
<p> WebSocket &#x662F;&#x4E00;&#x79CD;&#x7F51;&#x7EDC;&#x901A;&#x4FE1;&#x534F;&#x8BAE;&#xFF0C;&#x5F88;&#x591A;&#x9AD8;&#x7EA7;&#x529F;&#x80FD;&#x90FD;&#x9700;&#x8981;&#x5B83;&#x3002; </p>
</blockquote>
<h3 id="&#x4E00;&#x3001;&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;-websocket&#xFF1F;"><a name="&#x4E00;&#x3001;&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;-websocket&#xFF1F;" class="anchor-navigation-ex-anchor" href="#&#x4E00;&#x3001;&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;-websocket&#xFF1F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.1. &#x4E00;&#x3001;&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981; WebSocket&#xFF1F;</h3>
<p>&#x521D;&#x6B21;&#x63A5;&#x89E6; WebSocket &#x7684;&#x4EBA;&#xFF0C;&#x90FD;&#x4F1A;&#x95EE;&#x540C;&#x6837;&#x7684;&#x95EE;&#x9898;&#xFF1A;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x6709;&#x4E86; HTTP &#x534F;&#x8BAE;&#xFF0C;&#x4E3A;&#x4EC0;&#x4E48;&#x8FD8;&#x9700;&#x8981;&#x53E6;&#x4E00;&#x4E2A;&#x534F;&#x8BAE;&#xFF1F;&#x5B83;&#x80FD;&#x5E26;&#x6765;&#x4EC0;&#x4E48;&#x597D;&#x5904;&#xFF1F;</p>
<p>&#x7B54;&#x6848;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x56E0;&#x4E3A; HTTP &#x534F;&#x8BAE;&#x6709;&#x4E00;&#x4E2A;&#x7F3A;&#x9677;&#xFF1A;&#x901A;&#x4FE1;&#x53EA;&#x80FD;&#x7531;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x8D77;&#x3002;</p>
<p>&#x4E3E;&#x4F8B;&#x6765;&#x8BF4;&#xFF0C;&#x6211;&#x4EEC;&#x60F3;&#x4E86;&#x89E3;&#x4ECA;&#x5929;&#x7684;&#x5929;&#x6C14;&#xFF0C;&#x53EA;&#x80FD;&#x662F;&#x5BA2;&#x6237;&#x7AEF;&#x5411;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x3002;HTTP &#x534F;&#x8BAE;&#x505A;&#x4E0D;&#x5230;&#x670D;&#x52A1;&#x5668;&#x4E3B;&#x52A8;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x63A8;&#x9001;&#x4FE1;&#x606F;&#x3002;</p>
<h3 id="&#x4E8C;&#x3001;&#x7B80;&#x4ECB;"><a name="&#x4E8C;&#x3001;&#x7B80;&#x4ECB;" class="anchor-navigation-ex-anchor" href="#&#x4E8C;&#x3001;&#x7B80;&#x4ECB;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.2. &#x4E8C;&#x3001;&#x7B80;&#x4ECB;</h3>
<p>WebSocket &#x534F;&#x8BAE;&#x5728;2008&#x5E74;&#x8BDE;&#x751F;&#xFF0C;2011&#x5E74;&#x6210;&#x4E3A;&#x56FD;&#x9645;&#x6807;&#x51C6;&#x3002;&#x6240;&#x6709;&#x6D4F;&#x89C8;&#x5668;&#x90FD;&#x5DF2;&#x7ECF;&#x652F;&#x6301;&#x4E86;&#x3002;</p>
<p>&#x5B83;&#x7684;&#x6700;&#x5927;&#x7279;&#x70B9;&#x5C31;&#x662F;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x53EF;&#x4EE5;&#x4E3B;&#x52A8;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x63A8;&#x9001;&#x4FE1;&#x606F;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x4E5F;&#x53EF;&#x4EE5;&#x4E3B;&#x52A8;&#x5411;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x4FE1;&#x606F;&#xFF0C;&#x662F;&#x771F;&#x6B63;&#x7684;&#x53CC;&#x5411;&#x5E73;&#x7B49;&#x5BF9;&#x8BDD;&#xFF0C;&#x5C5E;&#x4E8E;<a href="https://en.wikipedia.org/wiki/Push_technology" target="_blank">&#x670D;&#x52A1;&#x5668;&#x63A8;&#x9001;&#x6280;&#x672F;</a>&#x7684;&#x4E00;&#x79CD;&#x3002;</p>
<p>&#x5176;&#x4ED6;&#x7279;&#x70B9;&#x5305;&#x62EC;&#xFF1A;</p>
<p>&#xFF08;1&#xFF09;&#x5EFA;&#x7ACB;&#x5728; TCP &#x534F;&#x8BAE;&#x4E4B;&#x4E0A;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7684;&#x5B9E;&#x73B0;&#x6BD4;&#x8F83;&#x5BB9;&#x6613;&#x3002;</p>
<p>&#xFF08;2&#xFF09;&#x4E0E; HTTP &#x534F;&#x8BAE;&#x6709;&#x7740;&#x826F;&#x597D;&#x7684;&#x517C;&#x5BB9;&#x6027;&#x3002;&#x9ED8;&#x8BA4;&#x7AEF;&#x53E3;&#x4E5F;&#x662F;80&#x548C;443&#xFF0C;&#x5E76;&#x4E14;&#x63E1;&#x624B;&#x9636;&#x6BB5;&#x91C7;&#x7528; HTTP &#x534F;&#x8BAE;&#xFF0C;&#x56E0;&#x6B64;&#x63E1;&#x624B;&#x65F6;&#x4E0D;&#x5BB9;&#x6613;&#x5C4F;&#x853D;&#xFF0C;&#x80FD;&#x901A;&#x8FC7;&#x5404;&#x79CD; HTTP &#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668;&#x3002;</p>
<p>&#xFF08;3&#xFF09;&#x6570;&#x636E;&#x683C;&#x5F0F;&#x6BD4;&#x8F83;&#x8F7B;&#x91CF;&#xFF0C;&#x6027;&#x80FD;&#x5F00;&#x9500;&#x5C0F;&#xFF0C;&#x901A;&#x4FE1;&#x9AD8;&#x6548;&#x3002;</p>
<p>&#xFF08;4&#xFF09;&#x53EF;&#x4EE5;&#x53D1;&#x9001;&#x6587;&#x672C;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x53D1;&#x9001;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x3002;</p>
<p>&#xFF08;5&#xFF09;&#x6CA1;&#x6709;&#x540C;&#x6E90;&#x9650;&#x5236;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x53EF;&#x4EE5;&#x4E0E;&#x4EFB;&#x610F;&#x670D;&#x52A1;&#x5668;&#x901A;&#x4FE1;&#x3002;</p>
<p>&#xFF08;6&#xFF09;&#x534F;&#x8BAE;&#x6807;&#x8BC6;&#x7B26;&#x662F;<code>ws</code>&#xFF08;&#x5982;&#x679C;&#x52A0;&#x5BC6;&#xFF0C;&#x5219;&#x4E3A;<code>wss</code>&#xFF09;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x7F51;&#x5740;&#x5C31;&#x662F; URL&#x3002;</p>
<h3 id="&#x4E09;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x7B80;&#x5355;&#x793A;&#x4F8B;"><a name="&#x4E09;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x7B80;&#x5355;&#x793A;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#&#x4E09;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x7B80;&#x5355;&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.3. &#x4E09;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x7B80;&#x5355;&#x793A;&#x4F8B;</h3>
<p>WebSocket &#x7684;&#x7528;&#x6CD5;&#x76F8;&#x5F53;&#x7B80;&#x5355;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x7F51;&#x9875;&#x811A;&#x672C;&#x7684;&#x4F8B;&#x5B50;&#xFF08;&#x70B9;&#x51FB;<a href="http://jsbin.com/muqamiqimu/edit?js,console" target="_blank">&#x8FD9;&#x91CC;</a>&#x770B;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF09;&#xFF0C;&#x57FA;&#x672C;&#x4E0A;&#x4E00;&#x773C;&#x5C31;&#x80FD;&#x660E;&#x767D;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ws = <span class="hljs-keyword">new</span> WebSocket(<span class="hljs-string">&quot;wss://echo.websocket.org&quot;</span>);

ws.onopen = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">evt</span>) </span>{ 
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Connection open ...&quot;</span>); 
  ws.send(<span class="hljs-string">&quot;Hello WebSockets!&quot;</span>);
};

ws.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">evt</span>) </span>{
  <span class="hljs-built_in">console</span>.log( <span class="hljs-string">&quot;Received Message: &quot;</span> + evt.data);
  ws.close();
};

ws.onclose = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">evt</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Connection closed.&quot;</span>);
};
</code></pre>
<h3 id="&#x56DB;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684;-api"><a name="&#x56DB;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684;-api" class="anchor-navigation-ex-anchor" href="#&#x56DB;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684;-api"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.4. &#x56DB;&#x3001;&#x5BA2;&#x6237;&#x7AEF;&#x7684; API</h3>
<p> WebSocket &#x5BA2;&#x6237;&#x7AEF;&#x7684; API &#x5982;&#x4E0B;&#x3002; </p>
<h6 id="41-websocket-&#x6784;&#x9020;&#x51FD;&#x6570;"><a name="41-websocket-&#x6784;&#x9020;&#x51FD;&#x6570;" class="anchor-navigation-ex-anchor" href="#41-websocket-&#x6784;&#x9020;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>4.1 WebSocket &#x6784;&#x9020;&#x51FD;&#x6570;</h6>
<p> WebSocket &#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x7528;&#x4E8E;&#x65B0;&#x5EFA; WebSocket &#x5B9E;&#x4F8B;&#x3002; </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ws = <span class="hljs-keyword">new</span> WebSocket(<span class="hljs-string">&apos;ws://localhost:8080&apos;</span>);
</code></pre>
<h6 id="42-websocketreadystate"><a name="42-websocketreadystate" class="anchor-navigation-ex-anchor" href="#42-websocketreadystate"><i class="fa fa-link" aria-hidden="true"></i></a>4.2 webSocket.readyState</h6>
<p> <code>readyState</code>&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x7684;&#x5F53;&#x524D;&#x72B6;&#x6001;&#xFF0C;&#x5171;&#x6709;&#x56DB;&#x79CD;&#x3002; </p>
<pre><code class="lang-tex">CONNECTING&#xFF1A;&#x503C;&#x4E3A;0&#xFF0C;&#x8868;&#x793A;&#x6B63;&#x5728;&#x8FDE;&#x63A5;&#x3002;
OPEN&#xFF1A;&#x503C;&#x4E3A;1&#xFF0C;&#x8868;&#x793A;&#x8FDE;&#x63A5;&#x6210;&#x529F;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x4FE1;&#x4E86;&#x3002;
CLOSING&#xFF1A;&#x503C;&#x4E3A;2&#xFF0C;&#x8868;&#x793A;&#x8FDE;&#x63A5;&#x6B63;&#x5728;&#x5173;&#x95ED;&#x3002;
CLOSED&#xFF1A;&#x503C;&#x4E3A;3&#xFF0C;&#x8868;&#x793A;&#x8FDE;&#x63A5;&#x5DF2;&#x7ECF;&#x5173;&#x95ED;&#xFF0C;&#x6216;&#x8005;&#x6253;&#x5F00;&#x8FDE;&#x63A5;&#x5931;&#x8D25;&#x3002;
</code></pre>
<p> &#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;&#x3002; </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">switch</span> (ws.readyState) {
  <span class="hljs-keyword">case</span> WebSocket.CONNECTING:
    <span class="hljs-comment">// do something</span>
    <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> WebSocket.OPEN:
    <span class="hljs-comment">// do something</span>
    <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> WebSocket.CLOSING:
    <span class="hljs-comment">// do something</span>
    <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> WebSocket.CLOSED:
    <span class="hljs-comment">// do something</span>
    <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">default</span>:
    <span class="hljs-comment">// this never happens</span>
    <span class="hljs-keyword">break</span>;
}
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; zhengyuliang all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x7F16;&#x5199;&#x65F6;&#x95F4;&#xFF1A;
2019-11-13 18:40:40
</span></footer></body></html>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../message/" class="navigation navigation-prev " aria-label="Previous page: Message">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="npm常用命令大全.html" class="navigation navigation-next " aria-label="Next page: Npm常用命令大全">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"WebSocket","level":"6.1","depth":1,"next":{"title":"Npm常用命令大全","level":"6.2","depth":1,"path":"technology/npm常用命令大全.md","ref":"technology/npm常用命令大全.md","articles":[]},"previous":{"title":"Message","level":"5.4","depth":1,"path":"message/README.md","ref":"message/README.md","articles":[]},"dir":"ltr"},"config":{"plugins":["sectionx","splitter","tbfed-pagefooter","toggle-chapters","theme-comscore","-lunr","-search","search-plus","donate","anchor-navigation-ex","expandable-chapters","chapter-fold","expandable-chapters-small","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy zhengyuliang","modify_label":"该文件编写时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"pluginsConfig":{"anchor-navigation-ex":{"showLevel":false,"showGoTop":true,"isRewritePageTitle":true,"isShowTocTitleIcon":true,"tocLevel1Icon":"fa fa-hand-o-right","tocLevel2Icon":"fa fa-hand-o-right","tocLevel3Icon":"fa fa-hand-o-right"}},"livereload":{},"splitter":{},"donate":{"title":"","button":"赏","wechat":"https://gitee.com/htzyl/img_bed/raw/master/pay/weixin-min.jpg","alipay":"https://gitee.com/htzyl/img_bed/raw/master/pay/alipay-min.jpg","wechatText":"微信打赏","alipayText":"支付宝打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"sectionx":{"tag":"b"},"highlight":{},"anchor-navigation-ex":{"showLevel":true,"associatedWithSummary":true,"mode":"float","showGoTop":true,"printLog":false,"multipleH1":true,"float":{"floatIcon":"fa fa-navicon","showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""},"pageTop":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""}},"theme-comscore":{},"expandable-chapters-small":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"toggle-chapters":{},"expandable-chapters":{},"search-plus":{}},"theme":"default","author":"zhengyuliang","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"记录技术文档内容","language":"zh-hans","gitbook":">=3.0.0","description":"记录Gitbook的配置和一些插件的使用","theme-default":{"showLevel":true}},"file":{"path":"technology/WebSocket.md","mtime":"2019-11-13T10:40:40.915Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-01-06T09:44:01.178Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sectionx/sectionx.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

